<template>
  <div
    class="remote"
    ref="remoteTable"
    :class="device == 'mobile' ? 'mobile-container' : 'pc-container'"
  >
    <!-- <div class="remote-header" style="text-align: right">
      <el-link type="primary">
        <i class="el-icon-rank"></i>
      </el-link>
    </div> -->
    <div style="margin-top: 8px" class="remote-box">
      <el-table
        ref="filterTable"
        row-class-name="custom-table-background"
        height="250"
        style="flex: 1"
        :data="tempData"
      >
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column prop="addrDesc" label="地址"> </el-table-column>
        <el-table-column fixed="right" width="60" label="操作">
          <template slot-scope="scope">
            <el-link type="primary">
              <i class="el-icon-s-help" @click="openDialog(scope.row)"> </i>
            </el-link>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align: right">
        <el-pagination
          background
          v-show="total > 0"
          layout="total, pager, next"
          :total="total"
          :small="device == 'mobile'"
          style="margin-top: 10px"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="pageSize"
          :current-page="currentPage"
        />
      </div>
    </div>
    <el-dialog title="反控权限校验" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input show-password v-model="form.password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="resetStatus">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { immsControlGet, immsControlById } from "@/api/home/main";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      tableData: [], //表格源数据
      tempData: [], //表格显示数据
      currentPage: 1, //当前页
      total: 0, //数据总数
      pageSize: 10, //一页展示的条数  默认一页展示10条，有很多数据就可以写其他的数
      inputContent: "", //搜索的数据值
      dialogVisible: false,
      showWarning: false,
      form: {
        name: "",
        password: "",
      },
      rules: {
        name: [{ required: true, message: "请输入用户", trigger: "blur" }],
        password: [{ required: true, message: "清输入密码", trigger: "blur" }],
      },
      tableRow: {},
      tableHight: 200,
    };
  },
  computed: {
    ...mapGetters(["sidebar", "avatar", "device"]),
  },
  async mounted() {
    // const height = this.$refs.remoteTable.offsetHeight;
    // this.tableHight = height - 300;
    let result = await immsControlGet();
    this.tableData = result.data;
    this.tempData = result.data;
    this.total = result.data.length;
    this.handleCurrentChange(1);
  },
  methods: {
    openDialog(row) {
      console.log(row);
      this.tableRow = row;
      this.dialogVisible = true;
    },
    // 切换状态
    resetStatus() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let { name, password } = this.form;
          let param = {
            user: name,
            password,
            devicename: this.tableRow.name,
          };
          console.log(param);
          let res = await immsControlById(param);
          let { code, msg } = res;
          if (code == 200) {
            this.$message({
              message: msg,
              type: "success",
            });
          } else {
            this.$message.error(msg);
          }
          this.dialogVisible = false;
          console.log(res);
        }
      });
    },
    //每页条数切换
    handleSizeChange(val) {
      this.pageSize = val;
      this.handleCurrentChange(1);
    },
    //页码切换
    handleCurrentChange(val) {
      this.currentPage = val;
      this.currentChangePage(this.tableData, val);
    },
    //分页方法
    currentChangePage(list, currentPage) {
      let from = (currentPage - 1) * this.pageSize;
      let to = currentPage * this.pageSize;
      this.tempData = []; //每次要置空
      for (; from < to; from++) {
        if (list[from] != null) {
          //只有当list的内容不为空的时候才插入，不然会显示空行
          this.tempData.push(list[from]);
        }
      }
    },
    searchput() {
      this.tempData = []; //每次要置空
      this.tempData = this.tableData.filter((item, index) => {
        return item.name.includes(this.inputContent);
      });
    },
  },
};
</script>

<style scoped lang="scss">
.mr-10 {
  margin-right: 4px;
}
.mr-16 {
  margin-right: 10px;
}
.btn-list {
  float: left;
  margin-bottom: 16px;
}
.mobile-container {
  ::v-deep .el-dialog {
    width: 95% !important;
    margin-top: 20% !important;
  }
  ::v-deep .el-table--scrollable-y .el-table__body-wrapper {
    height: 300px !important;
  }
}
.remote {
  height: calc(100% - 20px);
  overflow: hidden;
}
.remote-box {
  height: calc(100% - 10px);
  width: 100%;
  display: flex;
  flex-direction: column;
}
</style>
